﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2019-09-08
 * Time: 14:40
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;

using System.Collections.Generic;

namespace QStarter
{
	/// <summary>
	/// Description of ConfigForm.
	/// </summary>
	public partial class ConfigForm : Form
	{
		MainForm mainForm = null;
		
		public ConfigForm(Form f)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			
//			窗体设置
			this.mainForm = (MainForm)f;
			this.Owner = f;
			
//			初始化
			this.tbOpacity.Value = this.mainForm.ConfigToInt("Opacity");
			this.cbIconSize.SelectedIndex = this.cbIconSize.Items.IndexOf(this.mainForm.ConfigToInt("IconSize").ToString());
			this.tbHotKey.Text = this.mainForm.Configs["HotKey"].ToString();
			this.cbLocation.Checked = this.mainForm.isLocation > 0 ? false : true;
			this.cbReSize.Checked = this.mainForm.isResize > 0 ? false : true;
			this.cbFocusHide.Checked = this.mainForm.ConfigToInt("FocusHide") > 0 ? true : false;
			this.colorDialog.Color = Color.FromArgb(this.mainForm.ConfigToInt("ColorR"), this.mainForm.ConfigToInt("ColorG"), this.mainForm.ConfigToInt("ColorB"));
		}
		
//		透明度
		void TbOpacityValueChanged(object sender, EventArgs e)
		{
			this.mainForm.Opacity = (Double)this.tbOpacity.Value / 100;
			this.mainForm.Configs["Opacity"] = this.tbOpacity.Value;
			
			this.mainForm.IsConfigChange = true;
		}
		
//		图标大小
		void CbIconSizeSelectedIndexChanged(object sender, EventArgs e)
		{
			this.mainForm.Configs["IconSize"] = int.Parse(this.cbIconSize.Text);
			
			this.mainForm.IsConfigChange = true;
		}
		
//		热键输入
		void TbHotKeyKeyDown(object sender, KeyEventArgs e)
		{
			string modifiers = string.Empty;
			string keyValue = string.Empty;
			
			if (e.Modifiers != 0)
			{
				if (e.Control) { modifiers += "Ctrl + "; }
				else if (e.Alt) { modifiers += "Alt + "; }
				else if (e.Shift) { modifiers += "Shift + "; }
			}	
			
			if ((e.KeyValue >= 33 && e.KeyValue <= 40) || (e.KeyValue >= 65 && e.KeyValue <= 90) || (e.KeyValue >= 112 && e.KeyValue <= 123)) //a-z/A-Z/F1-F12
			{
				keyValue = e.KeyCode.ToString();
			}
			else if ((e.KeyValue >= 48 && e.KeyValue <= 57)) //0-9
			{
				keyValue = e.KeyCode.ToString().Substring(1);
			}
			
			this.tbHotKey.Text = (modifiers.Length == 0 || keyValue.Length == 0) ? "无" : modifiers + keyValue;
		}
		
//		禁止输入内容
		void TbHotKeyKeyPress(object sender, KeyPressEventArgs e) { e.Handled = true; }
		
//		热键设置
		void TbHotKeyKeyUp(object sender, KeyEventArgs e)
		{
			this.lbTitle.Focus();
			
			if (this.tbHotKey.Text.Length > 1)
			{
				string error = HotKey.RegKeyFromString(this.mainForm.Handle, 0, this.tbHotKey.Text);
				if (error.Length > 0)
				{
					MessageBox.Show(error);
					this.tbHotKey.Text = this.mainForm.Configs["HotKey"].ToString();
				}
			} 
			else
			{
				HotKey.UnRegKey(this.mainForm.Handle, 0);
			}
			
			this.mainForm.Configs["HotKey"] = this.tbHotKey.Text;
			
			this.mainForm.IsConfigChange = true;
		}
		
//		移动位置
		void CbLocationCheckedChanged(object sender, EventArgs e)
		{
			this.mainForm.isLocation = this.cbLocation.Checked ? 0 : 1;
			this.mainForm.Configs["isLocation"] = this.mainForm.isLocation;
			
			this.mainForm.IsConfigChange = true;
		}
		
//		调整大小
		void CbReSizeCheckedChanged(object sender, EventArgs e)
		{
			this.mainForm.isResize = this.cbReSize.Checked ? 0 : 1;
			this.mainForm.Configs["isResize"] = this.mainForm.isResize;
			
			this.mainForm.IsConfigChange = true;
		}
		
//		失去焦点隐藏
		void CbFocusHideCheckedChanged(object sender, EventArgs e)
		{
			this.mainForm.Configs["FocusHide"] = this.cbFocusHide.Checked ? 1 : 0;
			
			this.mainForm.IsConfigChange = true;
		}		
		
//		颜色选取
		void BtnColorClick(object sender, EventArgs e)
		{
			if (this.colorDialog.ShowDialog() == DialogResult.OK)
			{
				this.mainForm.BackColor = this.colorDialog.Color;
				this.mainForm.Configs["ColorR"] = this.colorDialog.Color.R;
				this.mainForm.Configs["ColorG"] = this.colorDialog.Color.G;
				this.mainForm.Configs["ColorB"] = this.colorDialog.Color.B;
				
				this.mainForm.IsConfigChange = true;
			}
		}
		
//		窗体关闭事件
		void BtnCloseClick(object sender, EventArgs e)
		{
			this.mainForm.CFClosed = true;
			this.mainForm.UpdateControls();
			this.Close();
		}
	}
}
